clear all; close all; clc
omega=2*pi*10;
phi=2*pi/3;
T=1e-3;
Td=1/(44.2e6);
t=Td:Td:T;
A=5;
NPSP=20;
TPSP=1e-3;
h=sign(randn(1,NPSP));
dt=0.5e-3



%plot(t);
omega0=2*pi*1.5e5;
PHI=omega0*t+omega*t+phi;
%plot(cos(PHI));


%S = A*cos(phi);
%plot(S);


m=fix(mod((t+dt)/TPSP,1)*NPSP)+1;

%plot(m);
%plot(t,h(m));
S=A*h(m).*cos(PHI);
%plot(S);

dtop=0;
mop=fix(mod((t+dtop)/TPSP,1)*NPSP)+1;
omegaop=2*pi*5;
Phiop=omega0*t+omegaop*t;
Sop=h(mop).*cos(Phiop);
%plot(Sop);

I=S*Sop.'
SopQ=h(mop).*sin(Phiop);
Q=S*SopQ'
B=atan(I/Q);

Omega=2*pi*[-1000:10:1000];
for i=1:length(Omega);
  PHI=omega0*t+Omega(i)*t+phi;  
  S=A*h(m).*cos(PHI);
  I(i)=S*Sop.';
  Q(i)=S*SopQ.';
end
%figure(1);
  %plot(Q,I);
  %figure(2);
  %plot(S);
  
  Amp=sqrt(I.^2+Q.^2);
  plot(omega-omegaop, Amp,omega-omegaop, A*length(S)/2*sinc((omega-omegaop)*T/2/pi));
    
    